package com.cn.stock.service.Impl;

import com.cn.api.PriceService;
import com.cn.dto.PriceDTO;
import com.cn.stock.entity.Price;
import com.cn.stock.mapper.PriceMapper;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Component
@RestController
@RequestMapping("/api/price")
public class PriceServiceImpl implements PriceService {

    @Resource
    PriceMapper priceMapper;

    private Price price = new Price();

    @Override
    @GetMapping(value = "/findAll")
    public List<PriceDTO> findAll(@RequestBody PriceDTO priceDTO) {
        price.setId(priceDTO.getId());
        price.setCostPrice(priceDTO.getCostPrice());
        price.setProductId(priceDTO.getProductId());
        price.setRetailPrice(priceDTO.getRetailPrice());
        price.setScribingPrice(priceDTO.getScribingPrice());
        return priceMapper.findAll(price);
    }

    @Override
    @PostMapping("/add")
    public int add(@RequestBody PriceDTO priceDTO) {
        price.setId(priceDTO.getId());
        price.setCostPrice(priceDTO.getCostPrice());
        price.setProductId(priceDTO.getProductId());
        price.setRetailPrice(priceDTO.getRetailPrice());
        price.setScribingPrice(priceDTO.getScribingPrice());
        return priceMapper.add(price);
    }

    @Override
    @DeleteMapping(value = "/delete")
    public int delete(@RequestBody PriceDTO priceDTO) {
        price.setId(priceDTO.getId());
        price.setCostPrice(priceDTO.getCostPrice());
        price.setProductId(priceDTO.getProductId());
        price.setRetailPrice(priceDTO.getRetailPrice());
        price.setScribingPrice(priceDTO.getScribingPrice());
        return priceMapper.delete(price);
    }

    @Override
    @PostMapping(value = "/update")
    public int update(@RequestBody PriceDTO priceDTO) {
        price.setId(priceDTO.getId());
        price.setCostPrice(priceDTO.getCostPrice());
        price.setProductId(priceDTO.getProductId());
        price.setRetailPrice(priceDTO.getRetailPrice());
        price.setScribingPrice(priceDTO.getScribingPrice());
        return priceMapper.update(price);
    }

    @Override
    @PostMapping(value = "/findByProductId")
    public PriceDTO findByProductId(@RequestParam int productId) {
        return priceMapper.findByProductId(productId);
    }
}
